package sharetime.playlist.domain;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;

import sharetime.tag.domain.TaggedEntityObject;
import sharetime.user.domain.UserData;

@Entity
@Table(name = "resource")
@Inheritance(strategy = InheritanceType.JOINED)
public class Resource extends TaggedEntityObject {

	private static final long serialVersionUID = -7769171652941118934L;
	
	protected String title;
	protected String author;
	protected String url;
	protected String note;
	protected String description;
	
	protected UserData owner;

	public Resource() {
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "fk_userdata_id")
	public UserData getOwner() {
		return owner;
	}

	public void setOwner(UserData owner) {
		this.owner = owner;
	}

	@Transient
	public String getAbsoluteUrl() throws RESyntaxException {
		RE matcher = new RE("://");
		if (matcher.match(url))
			return url;
		else
			return "http://" + url;
	}

	@Transient
	public String getDisplayName() throws RESyntaxException {
		RE matcher = new RE("http://([^/]*)/");
		if (matcher.match(url)) {
			return matcher.getParen(1);
		} else {
			return "external link";
		}
	}

	@Override
	public String toString() {
		return this.title;
	}
}
